package com.littcore.saap.system.service.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.bcel.generic.NEW;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.eventbus.Subscribe;
import com.littcore.common.BeanManager;
import com.littcore.dao.IResultsetTransformer;
import com.littcore.dao.page.IPageList;
import com.littcore.dao.ql.PageParam;
import com.littcore.eventbus.IEventBusListener;
import com.littcore.saap.assistant.vo.UserOpLogVo;
import com.littcore.saap.common.vo.IUserInfo;
import com.littcore.saap.core.event.BaseOpEvent;
import com.littcore.saap.core.web.util.LoginUtils;
import com.littcore.saap.system.dao.UserOpLogDao;
import com.littcore.saap.system.po.UserOpLog;
import com.littcore.saap.system.service.IUserInfoService;
import com.littcore.saap.system.service.IUserOpLogService;
import com.littcore.saap.system.webservice.IUserOpLogWebService;
import com.littcore.util.ArrayUtils;
import com.littcore.util.BeanCopier;

/**
 * .
 * 
 * <pre>
 * <b>描述：</b>
 * 
 * </pre>
 * 
 * <pre>
 * <b>修改记录：</b>
 * 
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">蔡源</a>
 * @since 2015年5月11日
 * @version 1.0
 */
public class UserOpLogServiceImpl implements IUserOpLogService, IUserOpLogWebService, IEventBusListener {
	@Resource
	private UserOpLogDao userOpLogDao;	
	@Resource
	private IUserInfoService userService;
	
	@Subscribe
  public void onEvent(BaseOpEvent event)
	{
	  UserOpLog opLog = new UserOpLog();
	  opLog.setRecordId(event.getRecordId());
    opLog.setFuncCode(event.getFuncCode());
    opLog.setModuleCode(event.getModuleCode());
    opLog.setOpBy(event.getOpBy());
    opLog.setOpDatetime(event.getOpDatetime());
    opLog.setTenantId(event.getTenantId());
    if(event.getDatas()!=null && !event.getDatas().isEmpty())
    {
      opLog.setParams(JSONArray.toJSONString(event.getDatas()));
    }   
    opLog.setOpContent("");
    opLog.setOpComment(event.getOpComment());
    userOpLogDao.save(opLog);
  }

	public IPageList listPage(PageParam pageParam) {
		String listHql = "select obj from UserOpLog obj" +
				"-- and obj.tenantId={tenantId}" + 
				"-- and obj.moduleCode={moduleCode}" + 
			    "-- and obj.recordId={recordId}";
		if(!StringUtils.isEmpty(pageParam.getCond("recordIds"))){
			listHql+="or obj.recordId in ("+pageParam.getCond("recordIds")+")";
		}

		return userOpLogDao.listPage(listHql, pageParam);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.littcore.saap.assistant.service.impl.IUserOpLogWebService#findPage(com
	 * .litt.core.dao.ql.PageParam)
	 */
	@Override
	public IPageList findPage(PageParam pageParam) {
		IPageList pageList = this.listPage(pageParam);
		
		//处理默认隐含参数
    final String contextPath = pageParam.getStringCond("contextPath");
		final Locale locale = LoginUtils.getLocale();
    
		pageList.setResultsetTransformer(new IResultsetTransformer() {

			@Override
			public List transform(List srcList) {
				List<UserOpLogVo> voList = BeanCopier.copyList(srcList, UserOpLogVo.class);
				for (UserOpLogVo userOpLogVo : voList) {
					IUserInfo userInfo = userService.find(userOpLogVo.getOpBy());
					userOpLogVo.setOpUserAvatar(userInfo.getHeadImgUrl());
					userOpLogVo.setAction(BeanManager.getMessage(userOpLogVo.getModuleCode() + ".func." + userOpLogVo.getFuncCode(), locale));
					userOpLogVo.setOpUsername(userInfo.getUserName());
					if(StringUtils.isEmpty(userOpLogVo.getOpContent()) && !StringUtils.isEmpty(userOpLogVo.getParams())){
					  Map<String, Object> params = (Map<String, Object>) JSON.parseObject(userOpLogVo.getParams(), LinkedHashMap.class);
					
					  Object[] values = params.values().toArray();
					  //处理默认隐含参数
					  values = ArrayUtils.add(values, 0, contextPath);
					  String opContent = BeanManager.getMessage(userOpLogVo.getModuleCode()+".func." + userOpLogVo.getFuncCode()+".log", values, locale);
					
					  userOpLogVo.setOpContent(opContent);
					}
				}
				return voList;
			}

		});
		return pageList;
	}

	@Override
	public void save(UserOpLogVo vo) {
		UserOpLog userOpLog = BeanCopier.copy(vo, UserOpLog.class);
		userOpLogDao.save(userOpLog);

	}

}
